package oa.web.service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import oa.core.Constant;
import oa.core.MySession;
import oa.web.dao.impl.LoginDao;
import oa.web.pojo.Employee;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginService {
	
	
	@Autowired
	private LoginDao<Employee> loginDao;
	
	
	public int isLogin(String username, String password, HttpServletRequest req){
		
		Employee emp = loginDao.getUserByUsername(username);
		
		if(emp == null){
			return Constant.LOGIN_STATUS_ERRORUSERNAME;
		}
		String md5_password = Constant.getInstance().toMd5(password);
		if(!password.equals(emp.getPass())){
			return Constant.LOGIN_STATUS_ERRORPASSWORD;
		}
		this.addUserToSession(req, emp);
		return Constant.LOGIN_STATUS_LOGIN_SUCCESS;
	}
	
	public void addUserToSession(HttpServletRequest req, Employee emp){
		HttpSession session =  MySession.getInstance(req);
		session.setAttribute(Constant.CURRENT_USER, emp);
	}
	
	public boolean login(String username, String password){
		String md5_password = Constant.getInstance().toMd5(password);
		//get real_mad5_password form database
		String real_mad5_password = "";
		if(md5_password.equals(real_mad5_password)){
			return true;
		}
		return false;
	}
}
